<%--
  Created by IntelliJ IDEA.
  User: Peter Zhou
 
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="com.zwy.Shopping.model.Category" %>
<%@ page import="com.zwy.Shopping.dao.ProductDao" %>
<%@ page import="java.util.List" %>
<%@ page import="com.zwy.Shopping.dao.impl.ProductDaoImpl" %>
<%@ page import="com.zwy.Shopping.model.Product" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page import="com.zwy.Shopping.model.CartItem" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html>
<script>
    function getProductIdToDel(productId){
        var xhr = new XMLHttpRequest();
        xhr.open("post","del_product_by_id",true);
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhr.onreadystatechange = function () {//回调函数,readystate改变调用
            if (xhr.readyState === 4 && xhr.status === 200) {
                //请求成功后,刷新当前页面,
                location.reload();
                // 请求已完成且成功
                console.log(xhr.responseText); // 打印服务器的响应内容
                // 在这里您可以处理响应，比如更新页面上的某些元素
            } else if (xhr.readyState === 4 && xhr.status !== 200) {
                // 请求已完成但出现了错误
                console.error("请求失败: " + xhr.statusText);
            }
        };
        xhr.send("productId=" + productId);
    }
    function getProductIdToModify(productId){
        var xhr = new XMLHttpRequest();
        xhr.open("post","get_modify_product_by_id",true);
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhr.onreadystatechange = function () {//回调函数,readystate改变调用
            if (xhr.readyState === 4 && xhr.status === 200) {
                //请求成功后,刷新当前页面,
                // 请求已完成且成功
                console.log(xhr.responseText); // 打印服务器的响应内容
                // 在这里您可以处理响应，比如更新页面上的某些元素
            } else if (xhr.readyState === 4 && xhr.status !== 200) {
                // 请求已完成但出现了错误
                console.error("请求失败: " + xhr.statusText);
            }
        };
        xhr.send("productId=" + productId);
    }
</script>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/bootstrap.bundle.js"></script>
    <meta charset="UTF-8">
    <title>管理员操作</title>
    <ul class="nav  border-bottom fixed-top" role="tablist">
        <li class="nav-item me-5 pe-5">
            <a class="nav-link " data-bs-toggle="pill" href="#home"><img class="img-fluid rounded" width="16px"
                                                                         height="16px" src="img/logo.png">服装购买系统管理员操作界面</a>
        </li>
        <li class="nav-item ms-5 ps-5">
            <a class="nav-link" href="admin_index.jsp" style="margin-left:500px; ">首页</a>
        </li>
        <li class="nav-item ms-5 ps-5">
            <a class="nav-link" href="index.jsp">切换为用户端</a>
        </li>
    </ul>
</head>
<style>
    .product_img{
        width: 80%;
        height: 60%;
    }
</style>
<body>
<%
    //设施request参数
    String search = request.getParameter("search");
    String categoryId = request.getParameter("categoryId");

    ProductDao productDao = new ProductDaoImpl();
    List<Category> categories = productDao.getAllCategories();
%>
<nav class="navbar navbar-expand-sm bg-primary navbar-dark " style="margin-top: 40px;">
    <ul class="navbar-nav">
        <li class="nav-item active">
            <a class="nav-link " href="admin_index.jsp">最近热买</a>
        </li>
        <%for (Category category : categories) {%>
        <li class="nav-item">
            <a class="nav-link " href="admin_index.jsp?categoryId=<%=category.getId()%>"><%=category.getName()%>
            </a>
        </li>
        <%}%>
    </ul>

    <!--        搜索框-->
    <form class="d-flex">
        <input class="form-control me-2" type="text" placeholder="查询商品">
        <button class="btn btn-primary" type="submit"><img class="rounded img-fluid bg-primary" src="img/search.png">
        </button>
    </form>
    <a class="btn btn-primary" href="add_product.jsp">添加商品</a>
</nav>
<div class="container-fluid mt-3">
    <div class="row">
        <% List<Product> products;
            if (search != null) {
                //模糊查询
                products = productDao.getProduct(search);
            } else if (categoryId != null) {
                products = productDao.getProduct(Integer.parseInt(categoryId));
            } else {
                products = productDao.getAllProduct();
            }
            //商品对象集不为空
            if (!products.isEmpty()) {
                for (Product p : products) {%>
        <div class="col-sm-4 ">
            <div class="card  m-3 bg-info ">

                <div class="card-img-top d-flex m-auto"><img class="product_img m-auto" src="img/<%=p.getImage()%>">
                </div>
                <div class="card-body">
                    <div class="card-title "><%=p.getName()%>
                    </div>
                    <div class="card-text" style="align-content: center">
                        <label>市价:<s><%=p.getMarketPrice()%>元</s></label>
                        <label>售价:<%=p.getPrice()%>元</label>
                        <label>销量:<%=p.getSales()%>件</label>
                        <label>仅剩:<%=p.getStock()%>件</label>
                    </div>
                    <div class="card-footer border-0 ">


                        <button class="btn btn-primary" onclick="getProductIdToDel(<%=p.getId()%>)">删除商品</button>
                        <a class="btn btn-secondary" href="modify_product.jsp" onclick="getProductIdToModify(<%=p.getId()%>)">修改商品信息</a>


                    </div>
                </div>
            </div>
        </div>
        <%}%>
        <%}%>

    </div>
</div>
</body>
</html>